import trinity
import blue
import audio2
import sys
import log
import uthread
import __builtin__
defaultAmmoColor = {'p': 'res:/Model/Turret/Ammo/p1.blue',
 'e': 'res:/Model/Turret/Ammo/ye1.blue',
 'h': 'res:/Model/Turret/Ammo/h1.blue'}

class TurretPair_sc2():

    def Initialize(self, graphicID, locatorA, locatorB, repeatDelay = 0, overrideBeamGraphicID = None):
        graphicID = graphicID.lower()
        self.graphicID = graphicID
        self.eveTurretPair = trinity.Load((('res:/dx9/Model/Turret/' + graphicID) + '.red'))
        self.eveTurretPair.locatorA = locatorA
        self.eveTurretPair.locatorB = locatorB
        self.weaponType = graphicID[2]
        self.stopShooting = False
        self.shootLoop = None
        self.repeatDelay = repeatDelay
        if (overrideBeamGraphicID is not None):
            fe = trinity.Load((('res:/Model/Effect3/Turret/' + overrideBeamGraphicID) + '.red'))
        else:
            fe = trinity.Load((('res:/Model/Effect3/Turret/' + graphicID) + '.red'))
        if not fe:
            fe = trinity.Load('res:/Model/Effect3/Turret/test.red')
        if fe:
            fe.source = None
            fe.dest = None
        srcAudio = audio2.AudEmitter(('effect_source_%s' % graphicID))
        destAudio = audio2.AudEmitter(('effect_dest_%s' % graphicID))
        if fe.sourceObject:
            obs = trinity.TriObserverLocal()
            obs.front = (0.0, -1.0, 0.0)
            obs.observer = srcAudio
            fe.sourceObject.observers.append(obs)
        if fe.destObject:
            obs = trinity.TriObserverLocal()
            obs.front = (0.0, -1.0, 0.0)
            obs.observer = destAudio
            fe.destObject.observers.append(obs)
        for eachSet in fe.curveSets:
            for eachCurve in eachSet.curves:
                if (eachCurve.__typename__ == 'TriEventCurve'):
                    if (eachCurve.name == 'audioEventsSource'):
                        eachCurve.eventListener = srcAudio
                    else:
                        if (eachCurve.name == 'audioEventsDest'):
                            eachCurve.eventListener = destAudio


        self.eveTurretPair.firingEffect = fe
        return self.eveTurretPair



    def Release(self):
        if (self.eveTurretPair is not None):
            self.eveTurretPair.locatorA = ''
            self.eveTurretPair.locatorB = ''
            self.eveTurretPair.target = None
            self.eveTurretPair = None



    def SetTarget(self, shipID, targetID):
        self.targetID = targetID
        self.shipID = shipID
        targetBall = sm.StartService('michelle').GetBall(targetID)
        if ((targetBall is not None) and hasattr(targetBall.model, 'TypeInfo')):
            if ('IEveSpaceObject2' in targetBall.model.TypeInfo()[1]):
                self.eveTurretPair.target = targetBall.model
            else:
                self.eveTurretPair.target = sm.StartService('damage').GetTargetLocators(shipID, targetID)[0]



    def SetAmmoColor(self, ammoTypeID):
        if (ammoTypeID is None):
            if (self.weaponType in defaultAmmoColor):
                gfxString = defaultAmmoColor[self.weaponType]
            else:
                return 
        else:
            gfxString = cfg.invtypes.Get(ammoTypeID).GraphicFile()
        if len(gfxString):
            col = eve.rot.GetInstance(gfxString)
            for x in self.eveTurretPair.firingEffect.Find('trinity.TriColorCurve'):
                if (x.name == 'Ammo'):
                    x.value = col.emissive




    def StartShooting(self, ammoGFXid = None):
        self.stopShooting = False
        if ((self.shootLoop is not None) and self.shootLoop.alive):
            return 
        if (self.repeatDelay > 0):
            self.shootLoop = uthread.new(self.ShootInLoop, self.repeatDelay)
        else:
            self.Shoot()



    def Shoot(self):
        now = blue.os.GetTime()
        self.eveTurretPair.shootingCurve.start = now
        for each in self.eveTurretPair.firingEffect.curveSets:
            each.Play()




    def ShootInLoop(self, delay):
        while not self.stopShooting:
            self.Shoot()
            blue.pyos.synchro.Sleep(self.repeatDelay)




    def Rest(self):
        self.targetID = None
        self.shipID = None
        if self.eveTurretPair:
            self.eveTurretPair.target = None
        self.stopShooting = True



    def ActivateFluff(self, fluffname, active):
        return 



    def StopShooting(self):
        self.targetID = None
        self.stopShooting = True



exports = {'turret.Turret_sc2': TurretPair_sc2}

